﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Services.Protocols;

namespace MedicWeb
{
    public class Global : System.Web.HttpApplication, IRequiresSessionState
    {

        protected void Application_Start(object sender, EventArgs e)
        {
        }

        protected void Session_Start(object sender, EventArgs e)
        {
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            HttpSessionState Session = HttpContext.Current.Session;
            try {
                object o = Session["init"];
            }catch(Exception exception) {
                Session = HttpContext.Current.Session;
            }
            Exception ex0 = Server.GetLastError();
            Exception ex = null;
            if (ex0 != null)
            {
                ex = ex0.InnerException;
            }
            Server.ClearError();
            string absolute = VirtualPathUtility.ToAbsolute("~");
            if (absolute == "/") {
                absolute = "";
            }
            if (ex != null)
            {
                if (ex is SoapException)
                {
                    SoapException se = (SoapException)ex;
                    if (se.Detail.InnerText == "AutontificationException")
                    {
                        Session["error"] = "Необходима аутентификация";
                        Response.Redirect(absolute+"/Login.aspx");
                        return;
                    } else if (se.Detail.InnerText == "AuthorizationException") {
                        Session["error"] = "У вас недостаточно прав";
                        Response.Redirect(absolute + "/Error.aspx");
                        return;
                    } else if (se.Detail.InnerText == "PrintTemplateException") {
                        Session["error"] = "Не определён активный шаблон печати";
                        Response.Redirect(absolute + "/Error.aspx");
                        return;
                    } else {
                        Session["error"] = "Проблемы с подключением к веб сервису<br />"
                            + se.Detail.InnerText + "<br />"
                            + ex.StackTrace.Normalize().Replace("\n", "<br />");
                        Exception innerException = ex.InnerException;
                        Response.Redirect(absolute + "/Error.aspx");
                        return;
                    }
                }
                //} else {
                Session["error"] = ex.StackTrace.Replace("\n", "<br />");
                Response.Redirect(absolute + "/Error.aspx");
                //}
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}